import {filedValidatesAPI} from '/src/api/crm/common.js'

import {isArray, isEmpty, isObject} from '/src/utils/types.js'
import {objDeepCopy} from '/src/utils/index.js'
// import GenerateRulesMixin from '/src/components/NewCom/WkForm/GenerateRules.js'
import {useHookGenerateRules} from "components/NewCom/WkForm/HookGenerateRules.js";
import { ElMessage } from 'element-plus'
// 用在Create.vue中，需要修改Create.vue中的minxs
export function useHookCustomFields(action, fieldRules1={}) {

  const hookGenerateRules = useHookGenerateRules()

  /**
   * 获取初始value值
   * detail 主要用于关联模块
   * type update 读取 value 判断 save 读取 defaultValue 判断
   */
  function getItemValue(item, detail, type) {
    console.log("getItemValue", item, detail,type)
    detail = detail || {}
    if (item.formType == 'contacts' ||
        item.formType == 'customer' ||
        item.formType == 'contract' ||
        item.formType == 'business'
    ) {
      // crm相关信息特殊处理
      if (type === 'update') {
        return item.value ? objDeepCopy(item.value) : []
      } else {
        const relativeData = detail[item.formType]
        return relativeData ? [relativeData] : []
      }
    } else if (item.formType == 'category') {
      // 产品类别
      return item.value ? item.value.map(item => parseInt(item)) : []
    } else if (item.formType == 'product') {
      // 关联产品
      return item.value
    } else if (item.formType == 'map_address') {
      if (type == 'update') {
        if (item.value.address) {
          item.value.address = item.value.address.split(',')
        }
        return item.value
      } else {
        return {}
      }
    } else if (item.formType == 'address') {
      // 地址
      if (type == 'update') {
        if (item.value) {
          if (typeof item.value === 'string') {
            const citys = item.value.split(',')
            return {
              province: citys[0] || '',
              city: citys[1] || '',
              area: citys[2] || ''
            }
          }
          return item.value
        } else {
          return {}
        }
      } else {
        return {}
      }
    } else if (item.formType == 'single_user') {
      if (type === 'update') {
        return isObject(item.value) && item.value.userId
            ? item.value.userId
            : ''
      } else {
        return isArray(item.defaultValue) && item.defaultValue.length > 0
            ? objDeepCopy(item.defaultValue[0]).userId
            : ''
      }
    } else if (item.formType == 'user' ||
        item.formType == 'structure' ||
        item.formType == 'file') {
      if (type === 'update') {
        return item.value ? objDeepCopy(item.value) : []
      } else {
        return item.defaultValue
            ? objDeepCopy(item.defaultValue)
            : []
      }
    } else {
      if (item.formType == 'number' ||
          item.formType == 'floatnumber' ||
          item.formType == 'percent') {
        if (type == 'update') {
          return isEmpty(item.value) ? undefined : item.value
        } else {
          return isEmpty(item.defaultValue) ? undefined : item.defaultValue
        }
      } else if (item.formType == 'detail_table') {
        const baseFieldList = isEmpty(item.value) ? [item.fieldExtendList] : item.value
        // 二维数组
        const tableValue = []
        baseFieldList.forEach(bigItem => {
          const subForm = {}
          tableValue.push(subForm)
          bigItem.forEach(subItem => {
            // 未转换 取 fieldName 值
            if ([
              'select',
              'checkbox'
            ].includes(subItem.formType)) {
              subItem.optionsData = null
            }
            subForm[subItem.fieldName] = getItemValue(subItem, null, type)
          })
        })
        return tableValue
      } else {
        if (type == 'update') {
          return item.value === null || item.value === undefined ? '' : item.value
        } else {
          return item.defaultValue === null || item.defaultValue === undefined ? '' : item.defaultValue
        }
      }
    }
  }

  /**
   * 验证唯一
   */
  function getUniquePromise(field, value, detail) {
    return new Promise((resolve, reject) => {
      var validatesParams = {}
      validatesParams.fieldId = field.fieldId
      validatesParams.value = getRealParams(field, value)
      if (detail.type == 'update') {
        validatesParams.batchId = detail.batchId
      }

      filedValidatesAPI(validatesParams).then(res => {
        // status 1 通过 0
        const resData = res.data.data || {}
        if (resData.status === 1) {
          resolve()
        } else {
          if (resData.ownerUserName) {
            reject(`${field.name}已存在，负责人为“${resData.ownerUserName}”`)
          } else if (resData.poolNames && resData.poolNames.length > 0) {
            reject(`${field.name}已存在，当前位于公海“${resData.poolNames.join('，')}”`)
          } else {
            reject(`${field.name}已存在`)
          }
        }
      }).catch(() => {
        reject()
      })
    })
  }

  /**
   * 获取字段默认内容
   */
  function getFormItemDefaultProperty(item, isCopy = true) {
    const temp = isCopy ? objDeepCopy(item) : item
    temp.field = item.fieldName

    // 细化 precisions
    if (item.formType === 'date_interval') {
      // 1 日期  2 时间日期
      if (item.precisions === 2) {
        temp.dateType = 'datetimerange'
        temp.dateValueFormat = 'YYYY.MM.DD HH:mm:ss'
      } else {
        temp.dateType = 'daterange'
        temp.dateValueFormat = 'YYYY.MM.DD'
      }
    } else if (item.formType === 'position') {
      // 1 省/地区、市、区/县、详细地址 2 省/地区、市、区/县
      // 3 省/地区、市 4 省/地区
      temp.showDetail = item.precisions === 1
      temp.hideArea = item.precisions === 3 || item.precisions === 4
      temp.onlyProvince = item.precisions === 4
    } else if (item.formType === 'detail_table') {
      // 校准默认单元数据
      // authLevel 1 不能查看不能编辑 2可查看  3 可编辑可查看
      const fieldForm = {}
      temp.fieldExtendList.forEach(extendItem => {
        const copyExtendItem = objDeepCopy(extendItem)
        getFormItemDefaultProperty(extendItem, false)
        extendItem.show = extendItem.isHidden !== 1
        if (extendItem.show) {
          extendItem.rules = hookGenerateRules.getRules(copyExtendItem)
        }
        getItemRadio(extendItem, extendItem)
        fieldForm[extendItem.fieldName] = getItemValue(extendItem)
      })
      temp.fieldForm = fieldForm // 用于追加新事项

      const fieldExtendList = objDeepCopy(item.fieldExtendList)
      const baseFieldList = isEmpty(item.value) ? [fieldExtendList] : item.value
      // 二维数组
      const tableFieldList = []
      baseFieldList.forEach(bigItem => {
        const subValue = []
        bigItem.forEach(subItem => {
          const copySubItem = objDeepCopy(subItem)
          const subTemp = getFormItemDefaultProperty(subItem, false)
          // 特殊字段允许多选
          getItemRadio(subItem, subTemp)
          subTemp.show = subTemp.isHidden !== 1
          if (subTemp.show) {
            subTemp.rules = hookGenerateRules.getRules(copySubItem)
          }
          subValue.push(subTemp)
        })
        tableFieldList.push(subValue)
      })
      temp.fieldList = tableFieldList
    }
    return temp
  }

  /**
   * 获取二维数组字段
   * @param {*} array
   * @param {*} formType
   */
  function getItemWithFromType(array, formType) {
    let item = null
    for (let index = 0; index < array.length; index++) {
      const children = array[index]
      for (let childIndex = 0; childIndex < children.length; childIndex++) {
        const element = children[childIndex]
        if (element.formType === formType) {
          item = element
          break
        }
      }

      if (item) {
        break
      }
    }

    return item
  }

  /**
   * 循环二维数组
   * @param {*} array
   * @param {*} result
   */
  function itemsForEach(array, callback) {
    for (let index = 0; index < array.length; index++) {
      const children = array[index]
      if (isArray(children)) {
        for (let childIndex = 0; childIndex < children.length; childIndex++) {
          const element = children[childIndex]
          callback(element, index, childIndex, children)
        }
      } else {
        callback(children, index)
      }
    }
  }

  /**
   * 获取字段是否可编辑
   */
  function getItemIsCanEdit(item, type) {
    if (isEmpty(item.authLevel)) return true
    // authLevel 1 不能查看不能编辑 2可查看  3 可编辑可查看
    return (type === 'update' && item.authLevel == 3) || type !== 'update'
  }

  /**
   * user single_user structure
   */
  function getItemRadio(field, data) {
    if (field.formType == 'user' || field.formType == 'structure') {
      data.radio = false
    } else if (field.formType == 'single_user') {
      data.radio = true
    }
  }

  /**
   * 获取error错误
   */
  function getFormErrorMessage(crmForm) {
    // 提示第一个error
    if (crmForm.fields) {
      for (
          let index = 0;
          index < crmForm.fields.length;
          index++
      ) {
        const ruleField = crmForm.fields[index]
        if (ruleField.validateState == 'error') {
          // this.$message.error(ruleField.validateMessage)
          ElMessage.error({message: ruleField.validateMessage})
          break
        }
      }
    }
  }

  /**
   * 获取相关联item
   */
  function getItemRelativeInfo(list, fromType) {
    let crmItem = null
    if (action.value.type == 'relative') {
      crmItem = action.value.data[fromType]
    } else {
      const childIsArray = list.length > 0 && isArray(list[0])
      const crmObj = childIsArray ? getItemWithFromType(list, fromType) : list.find(listItem => {
        return listItem.formType === fromType
      })

      if (crmObj && crmObj.value && crmObj.value.length > 0) {
        crmItem = crmObj.value[0]
      }
    }
    return crmItem
  }

  /**
   * 获取提交参数
   * otherKeys 该值也增加到对象里
   */
  function getSubmitParams(array, data, otherKeys) {
    console.log("getSubmitParams",array,data,otherKeys)
    var params = {entity: {}, field: []}
    for (let index = 0; index < array.length; index++) {
      const field = array[index]
      let dataValue = null
      if (field.hasOwnProperty('show')) {
        dataValue = field.show ? data[field.fieldName] : null
      } else {
        dataValue = data[field.fieldName]
      }
      if (field.formType == 'product') {
        getProductParams(params, dataValue)
      } else if (field.formType == 'map_address') {
        getCustomerAddressParams(params.entity, dataValue)
      } else if (field.fieldType == 1) {
        const fieldValue = getRealParams(field, dataValue)
        params.entity[field.fieldName] = isEmpty(fieldValue) ? '' : fieldValue
      } else if (field.formType !== 'desc_text') { //  描述文字忽略
        const valueObj = {
          fieldName: field.fieldName,
          fieldType: field.fieldType,
          name: field.name,
          type: field.type,
          fieldId: field.fieldId,
          value: getRealParams(field, dataValue)
        }
        if (isArray(otherKeys)) {
          otherKeys.forEach(key => {
            valueObj[key] = field[key]
          })
        }
        params.field.push(valueObj)
      }
    }
    return params
  }

  /**
   * 获取产品提交参数
   */
  function getProductParams(params, dataValue) {
    if (dataValue) {
      params['product'] = dataValue.product ? dataValue.product.map(item => {
        item.salesPrice = item.salesPrice == '' ? 0 : item.salesPrice
        item.num = item.num == '' ? 0 : item.num
        item.discount = item.discount == '' ? 0 : item.discount
        return item
      }) : []
      params.entity['totalPrice'] = dataValue.totalPrice
          ? dataValue.totalPrice
          : 0
      params.entity['discountRate'] = dataValue.discountRate
          ? dataValue.discountRate
          : 0
    } else {
      params['product'] = []
      params.entity['totalPrice'] = ''
      params.entity['discountRate'] = ''
    }
  }

  /**
   * 获取客户位置提交参数
   */
  function getCustomerAddressParams(params, dataValue) {
    if (dataValue) {
      params['address'] = dataValue.address
          ? dataValue.address.join(',')
          : ''
      params['detailAddress'] = dataValue.detailAddress
      params['location'] = dataValue.location
      params['lng'] = dataValue.lng
      params['lat'] = dataValue.lat
    } else {
      params['address'] = ''
      params['detailAddress'] = ''
      params['location'] = ''
      params['lng'] = ''
      params['lat'] = ''
    }
  }

  /**
   * 关联客户 联系人等数据要特殊处理
   */
  function getRealParams(field, dataValue) {
    if (
        field.fieldName == 'customerId' ||
        field.fieldName == 'contactsId' ||
        field.fieldName == 'businessId' ||
        field.fieldName == 'leadsId' ||
        field.fieldName == 'contractId') {
      if (isEmpty(dataValue)) return ''
      if (isArray(dataValue) && dataValue.length > 0 && isObject(dataValue[0])) {
        return dataValue[0][field.fieldName]
      }
      if (isArray(dataValue)) return dataValue[dataValue.length - 1]
      if (['string', 'number'].includes(typeof dataValue)) return dataValue
      return ''
    } else if (
        field.formType == 'user' ||
        field.formType == 'structure'
    ) {
      let newDataValue = dataValue || []
      if (isArray(dataValue) && dataValue.length > 0) {
        if (isObject(dataValue[0])) {
          newDataValue = dataValue.map(valueItem => field.formType == 'user'
              ? valueItem.userId
              : valueItem.id)
        }
      }
      return isArray(newDataValue) ? newDataValue.join(',') : newDataValue
    } else if (field.formType == 'file') {
      if (dataValue && dataValue.length > 0) {
        return dataValue[0].batchId
      }
      return ''
    } else if (field.formType == 'category') {
      if (isEmpty(dataValue)) return ''
      if (!isArray(dataValue)) return dataValue
      if (dataValue && dataValue.length > 0) {
        return dataValue[dataValue.length - 1]
      }
      return ''
    } else if (field.formType == 'checkbox') {
      if (isArray(dataValue)) {
        return dataValue.join(',')
      }
      return dataValue
    } else if (field.formType == 'select') {
      return dataValue
    } else if (field.formType == 'detail_table') {
      const fieldExtendList = field.fieldExtendList || []
      const values = []
      const tableValues = Array.isArray(dataValue) ? dataValue : []

      for (let tableValueIndex = 0; tableValueIndex < tableValues.length; tableValueIndex++) {
        const tableValue = tableValues[tableValueIndex]
        // 去除空值数据
        if (!getFormValueIsEmpty(fieldExtendList, tableValue)) {
          const valuesItems = []
          fieldExtendList.forEach(tableField => {
            const copyTableField = objDeepCopy(tableField)
            delete copyTableField.companyId
            copyTableField.value = getRealParams(copyTableField, tableValue[copyTableField.fieldName])
            valuesItems.push(copyTableField)
          })
          values.push(valuesItems)
        }
      }

      return values
    }

    return dataValue
  }

  /**
   * 判断对象值是否是空
   */
  function getFormValueIsEmpty(fieldList, valueObj) {
    for (let index = 0; index < fieldList.length; index++) {
      const field = fieldList[index]
      const value = valueObj[field.fieldName]
      if (field.formType === 'location') {
        if (isObject(value) && (!isEmpty(value.lat) || !isEmpty(value.lng) || !isEmpty(value.address))) {
          return false
        }
      } else if (!isEmpty(value)) {
        return false
      }
    }
    return true
  }

  /**
   * 获取逻辑表单隐藏id
   */
  function getFormAssistIds(list, valueObj) {
    let allIds = []
    itemsForEach(list, item => {
      if ([
            'select',
            'checkbox'
          ].includes(item.formType) &&
          item.remark === 'options_type' &&
          item.optionsData) {
        for (const key in item.optionsData) {
          allIds = allIds.concat(item.optionsData[key] || [])
        }
      }
    })

    allIds = allIds.filter(o => Boolean(o))
    allIds = Array.from(new Set(allIds))

    const ignoreIds = []
    getFormAssistData(list, valueObj, allIds, ignoreIds)
    return allIds.filter(o => !ignoreIds.includes(o))
  }

  /**
   * 获取信息
   */
  function getFormAssistData(list, valueObj, allIds, ignoreIds) {
    // let ignorIds = []
    const ignoreLength = ignoreIds.length
    itemsForEach(list, item => {
      if ([
            'select',
            'checkbox'
          ].includes(item.formType) &&
          item.remark === 'options_type' &&
          item.optionsData) {
        let value = valueObj ? valueObj[item.field || item.fieldName] : item.value
        if (!allIds.includes(item.formAssistId)) {
          if (item.formType === 'select') {
            if (isEmpty(value)) {
              value = []
            } else {
              const findRes = item.setting.find(o => {
                return isObject(o) ? o.value === value : o === value
              })
              if (findRes) {
                const _val = isObject(findRes) ? findRes.value : findRes
                value = [_val]
              } else {
                value = ['其他']
              }
              // value = item.setting.includes(value) ? [value] : ['其他']
            }
          } else if (item.formType === 'checkbox') {
            if (isArray(value)) {
              const copyValue = objDeepCopy(value)
              const otherItem = copyValue.filter((name) => !item.setting.includes(name))
              if (otherItem.length > 0) {
                const newValue = copyValue.filter((name) => !otherItem.includes(name))
                newValue.push('其他')
                value = newValue
              }
            } else {
              value = []
            }
          }

          for (const key in item.optionsData) {
            // if (value && value.includes(key)) {
            if (value) {
              const findIndex = value.findIndex(o => o == key)
              if (findIndex === -1) continue

              const keyValue = item.optionsData[key] || []
              for (let index = 0; index < keyValue.length; index++) {
                const id = keyValue[index]
                if (!ignoreIds.includes(id)) {
                  ignoreIds.push(id)
                }
              }
            }
          }
        }
      }
    })

    if (ignoreLength !== ignoreIds.length) {
      const newAllIds = allIds.filter(o => !ignoreIds.includes(o))
      getFormAssistData(list, valueObj, newAllIds, ignoreIds)
    }
  }

  /**
   * 获取表单展示内容
   * data 用于相关模块新建填充模块值
   * type 新建编辑
   */
  function getFormContentByOptionsChange(fieldList, formObj, rules = {}, data = {}, type) {
    const allFieldRules = fieldRules1 || rules
    const actionData = action.value && action.value.data ? action.value.data : data
    const actionType = action.value && action.value.type ? action.value.type : type

    const fieldRules = {}
    const fieldForm = {}

    // 依据最新的值，获取隐藏的ids
    const assistIds = getFormAssistIds(fieldList, formObj)

    itemsForEach(fieldList, fieldItem => {
      fieldItem.show = !assistIds.includes(fieldItem.formAssistId)
      // 展示 并且 允许编辑，加入验证规则
      if (fieldItem.show && !fieldItem.disabled) {
        if (allFieldRules[fieldItem.field]) {
          fieldRules[fieldItem.field] = allFieldRules[fieldItem.field]
        } else {
          fieldRules[fieldItem.field] = hookGenerateRules.getRules(fieldItem)
        }
      }

      // 值获取
      if (fieldItem.show) {
        if (isEmpty(formObj[fieldItem.field])) {
          fieldForm[fieldItem.field] = getItemValue(fieldItem, actionData, actionType)
        } else {
          fieldForm[fieldItem.field] = formObj[fieldItem.field]
        }
      }
    })

    return {
      fieldForm,
      fieldRules
    }
  }


  return {
    hookGenerateRules,
    getItemValue,
    getUniquePromise,
    getFormItemDefaultProperty,
    getItemWithFromType,
    itemsForEach,
    getItemIsCanEdit,
    getItemRadio,
    getFormErrorMessage,
    getItemRelativeInfo,
    getSubmitParams,
    getProductParams,
    getCustomerAddressParams,
    getRealParams,
    getFormValueIsEmpty,
    getFormAssistIds,
    getFormAssistData,
    getFormContentByOptionsChange
  }
}